package com.mspbots.core.wise.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mspbots.core.AbstractBaseController;
import com.mspbots.core.wise.model.TenantTimeEntry;
import com.mspbots.core.wise.model.dto.TimeEntryBotReport;
import com.mspbots.core.wise.service.TenantTimeEntryService;
import com.mspbots.cw.request.TenantTimeEntryDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

/**
 * @author David Yang
 * @date Created by 2019/6/5
 */
@RestController
@RequestMapping("{tenant}/user-time-entry")
public class TenantUserTimeEntryController extends AbstractBaseController<TenantTimeEntry, TenantTimeEntryService> {


    @GetMapping("/analysis-list")
    public Callable list(TenantTimeEntryDTO query) {
        return () -> service.analysisList(query);
    }

    @GetMapping("/list-id-entrydate")
    public Callable listbyIdAndEntrydate(TenantTimeEntryDTO query) {
        return () -> service.listbyIdAndEntrydate(query);
    }

    @PostMapping("add-batch")
    public Callable addAll(@RequestBody List<TenantTimeEntryDTO> entries) {
        return () -> {
            List<TenantTimeEntry> tenUserTimeEntries = entries.stream().map(entry -> {
                TenantTimeEntry tenUserTimeEntry = new TenantTimeEntry();
                BeanUtils.copyProperties(entry, tenUserTimeEntry);
                return tenUserTimeEntry;
            }).collect(Collectors.toList());
            this.service.saveBatch(tenUserTimeEntries);
            return entries;
        };
    }

    @GetMapping("/tenant-user-list")
    public Callable listByTenantUser(TenantTimeEntryDTO query) {
        return () -> service.listByTenantUser(query);
    }


    @GetMapping("last-one")
    public Callable lastOne(@PathVariable Long tenant) {
        return () -> this.service.lastOne(tenant);
    }

    @GetMapping("findLastOneById")
    public Callable findLastOneById(TenantTimeEntryDTO query) {
        return () -> this.service.findLastOneById(query);
    }

    @GetMapping("findByChargeToId")
    public Callable findByChargeToId(TenantTimeEntry query) {
        return () -> this.service.findByChargeToId(query);
    }

    @GetMapping(path = "/list-sort")
    public Callable selectListBySort(TenantTimeEntry queryParams) {
        return () -> {
            QueryWrapper<TenantTimeEntry> queryWrapper = new QueryWrapper<>(queryParams);
            List<TenantTimeEntry> list = service.list(queryWrapper);
            list.sort(Comparator.comparing(TenantTimeEntry::getEnteredDate).reversed());
            return list;
        };
    }

    @GetMapping(path = "/bot-report")
    public Callable botReport(Page<TimeEntryBotReport> page, TimeEntryBotReport queryParams) {
        return () -> service.botReport(page, queryParams);
    }
}
